package com.heima.kafka.controller;

import cn.hutool.json.JSONUtil;
import com.heima.kafka.dto.UserDTO;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class HelloController {
    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("hello")
    public String helloKafka(@RequestParam("msg") String msg) {
        kafkaTemplate.send("itcast_heima", msg);
        return "punlisher send msg " + msg;
    }

    @GetMapping("user")
    public String helloKafka(@RequestBody UserDTO userDTO) {
        String msg = JSONUtil.toJsonStr(userDTO);
        kafkaTemplate.send("itcast_heima_user", msg);
        return "punlisher send msg " + msg;
    }
}
